package com.pigplay.demo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.pagehelper.PageHelper;
import com.pigplay.demo.dao.TUserDao;
import com.pigplay.demo.model.TUser;
import com.pigplay.demo.service.TUserService;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private TUserService userService;
	
	@RequestMapping("/list")
    public String listUsers(Model model, Integer page) {
		page = page == null ? 1 : page;
		PageHelper.startPage(page, 2);
		List<TUser> u = userService.selectUsers();
		model.addAttribute("users", u);
        return "/user/list";
    }
	
	@RequestMapping("/add")
    public String add() {
        return "/user/add";
    }
	
	@RequestMapping("/save")
    public String save(Model model, String username, String password) {
		userService.insertUser(new TUser(username, password));
		PageHelper.startPage(1, 2);
		model.addAttribute("users", userService.selectUsers());
		return "/user/list";
    }
	
	private void test(){}
}
